package at.jku.se.fastsell.gui;

import at.jku.se.fastsell.FastsellUI;
import at.jku.se.fastsell.data.Auction;
import at.jku.se.fastsell.data.DataAccess;

import com.vaadin.data.util.converter.StringToDoubleConverter;
import com.vaadin.data.util.converter.StringToIntegerConverter;
import com.vaadin.data.validator.DoubleRangeValidator;
import com.vaadin.data.validator.IntegerRangeValidator;
import com.vaadin.server.VaadinSession;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Notification;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.Reindeer;

public class NewAuctionView extends MainView implements IViewStatus {

	private static final long serialVersionUID = 1L;

	public static String NAME = "NewAuction";
	
	private TextField txtTitle, txtDescription, txtPrice, txtDuration;
	private Label lblHead;
	private VerticalLayout mainLayout;
	private HorizontalLayout buttonLayout;
	private FormLayout formLayout;
	private Button btnSell, btnBack;

	// ----------------------------------------------------------------------------------

	public NewAuctionView() {
		buildMainLayout();
		this.setContent(mainLayout);
	}
	
	// ----------------------------------------------------------------------------------

	private void buildMainLayout() {
		mainLayout = new VerticalLayout();
		
		lblHead = new Label("Create new auction");
		lblHead.setStyleName(Reindeer.LABEL_H2);
		mainLayout.addComponent(lblHead);
		
		formLayout = new FormLayout();
		formLayout.setSpacing(true);
		mainLayout.addComponent(formLayout);
		
		txtTitle = new TextField("Title");
		txtTitle.setRequired(true);
		txtTitle.setMaxLength(24);
		formLayout.addComponent(txtTitle);

		txtDescription = new TextField("Description");
		txtDescription.setRequired(true);
		txtDescription.setMaxLength(400);
		formLayout.addComponent(txtDescription);

		txtPrice = new TextField("Starting price");
		txtPrice.setRequired(true);
		txtPrice.setMaxLength(8);
		txtPrice.setConverter(new StringToDoubleConverter());
		txtPrice.addValidator(new DoubleRangeValidator("Please enter a number from 1-1000", 1d, 1000d));
		txtPrice.setValue("1");
		formLayout.addComponent(txtPrice);

		txtDuration = new TextField("Duration [days]");
		txtDuration.setRequired(true);
		txtDuration.setMaxLength(2);
		txtDuration.setConverter(new StringToIntegerConverter());
		txtDuration.addValidator(new IntegerRangeValidator("Please enter a number from 1-7", 1, 7));
		txtDuration.setValue("1");
		formLayout.addComponent(txtDuration);
		
		buttonLayout = new HorizontalLayout();
		buttonLayout.setSpacing(true);
		mainLayout.addComponent(buttonLayout);

		btnSell = new Button("Sell it");
		btnSell.addClickListener(new ClickListener() {

			private static final long serialVersionUID = 1L;

			@Override
			public void buttonClick(ClickEvent event) {
				try {
					if (txtTitle.isValid() && txtDescription.isValid() && txtPrice.isValid() && txtDuration.isValid()) {
					String title = txtTitle.getValue();
					String description = txtDescription.getValue();
					String seller = VaadinSession.getCurrent().getSession().getAttribute("user").toString();
					double price = Double.parseDouble(txtPrice.getValue());
					int duration = Integer.parseInt(txtDuration.getValue());

					Auction a = DataAccess.createAuction(title, description, seller, price, duration);
					Notification.show("Auction '" + a.getAuctionID() + "' created successfully");
					clearAllFields();
					} else {
						Notification.show("Please check your input", Notification.Type.ERROR_MESSAGE);
					}
				} catch (Exception e) {
					Notification.show("Could not create auction: " + e.toString(), Notification.Type.ERROR_MESSAGE);
				}
			}
		});
		buttonLayout.addComponent(btnSell);
		
		btnBack = new Button("Back");
		btnBack.addClickListener(new ClickListener() {
			
			private static final long serialVersionUID = 1L;

			@Override
			public void buttonClick(ClickEvent event) {
				FastsellUI.navigateTo(AllAuctionsView.NAME);
			}
		});
		buttonLayout.addComponent(btnBack);
	}
	
	private void clearAllFields() {
		txtTitle.setValue("");
		txtDescription.setValue("");
		txtPrice.setValue("1");
		txtDuration.setValue("1");
	}
	
	// ----------------------------------------------------------------------------------

	@Override
	public void setInactive() {
		// nothing to do
		
	}

	@Override
	public String getName() {
		return NAME;
	}

}
